import 'package:flutter/material.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:flutter_oldman_health/api/constant.dart';
import 'package:flutter_oldman_health/widget/common_container.dart';
import 'map_base_page_state.dart';

class MapAddressSelectPage extends StatefulWidget {
  BMFCoordinate selectGeoPt;
//this.customMapOptions,
  MapAddressSelectPage(this.selectGeoPt);

  @override
  MapAddressSelectPageState createState() {
    return MapAddressSelectPageState();
  }
}

class MapAddressSelectPageState extends BMFBaseMapState<MapAddressSelectPage> {
  BMFMarker marker;
  BMFMarker selectBMFMarker;
  BMFCoordinate clickedGeoPt;
  BMFMapOptions customMapOptions;

  String title = "请长按地图选择区域中心坐标";

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return MaterialApp(
        home: Scaffold(
            body: Container(
                color: Constant.backgroundColor,
                child: Column(children: [
                  _getTitle("区域防护设置"),
                  Expanded(
                      flex: 1,
                      child: Container(
                          child: Stack(children: <Widget>[
                        generateMap(),
                        Align(
                            alignment: Alignment.topCenter,
                            child: CommonContainer(
                                Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Icon(
                                        Icons.touch_app,
                                        color: Colors.grey,
                                      ),
                                      Text(title)
                                    ]),
                                padding: EdgeInsets.fromLTRB(8, 12, 8, 12)))
                      ])))
                ]))));
  }

  /// 创建完成回调
  @override
  void onBMFMapCreated(BMFMapController controller) {
    super.onBMFMapCreated(controller);

    /// 地图加载回调
    myMapController.setMapDidLoadCallback(callback: () {
      print('mapDidLoad-地图加载完成');
      myMapController.showUserLocation(true);
      updateUserLocation();
      myMapController.setUserTrackingMode(BMFUserTrackingMode.Follow);
    });
    if (widget.selectGeoPt != null) {
      marker = BMFMarker(
          position: widget.selectGeoPt,
          title: '区域中心',
          subtitle: '',
          identifier: 'select_marker',
          icon: 'images/icon_binding_point.png',
          canShowCallout:false,
          enabled: false,
          enabled3D:false,
          selected: false,
          draggable: false);
      myMapController.addMarker(marker);
    }

    myMapController.setMapOnLongClickCallback(callback: (coordinate) {
      clickedGeoPt = coordinate;
      setState(() {
        title = coordinate.latitude.toString() + "  ${coordinate.longitude}";
      });
      if (selectBMFMarker != null) {
        // myMapController.removeMarker(selectBMFMarker);
        selectBMFMarker.updatePosition(coordinate);
      } else {
        selectBMFMarker = BMFMarker(
            position: clickedGeoPt,
            title: '选中区域',
            subtitle: '',
            identifier: 'select_marker',
            icon: 'images/icon_mark.png',
            enabled: false,
            draggable: false);
        myMapController.addMarker(selectBMFMarker);
      }
    });
  }

  /// 创建地图
  Container generateMap() {
    return Container(
      height: screenSize.height,
      width: screenSize.width,
      child: BMFMapWidget(
        onBMFMapCreated: onBMFMapCreated,
        mapOptions: initMapOptions(),
      ),
    );
  }

  /// 设置地图参数
  @override
  BMFMapOptions initMapOptions() {
    BMFCoordinate center = widget.selectGeoPt;
    BMFMapOptions mapOptions = BMFMapOptions(
        // mapType: mapType,
        zoomLevel: 14,
        maxZoomLevel: 21,
        minZoomLevel: 4,
        logoPosition: BMFLogoPosition.LeftBottom,
        mapPadding: BMFEdgeInsets(top: 0, left: 50, right: 50, bottom: 0),
        overlookEnabled: true,
        overlooking: -15,
        center: center);
    return mapOptions;
  }


  Widget _getTitle([String title = "区域防护设置"]) {
    return Container(
        color: Constant.appbarColor,
        padding: EdgeInsets.fromLTRB(16, 46, 16, 16),
        child: FractionallySizedBox(
            widthFactor: 1,
            child: Stack(alignment: Alignment.center, children: [
              Positioned(
                  left: 2,
                  child: GestureDetector(
                      behavior: HitTestBehavior.translucent,
                      onTap: () {
                        Navigator.pop(context);
                      },
                      child: Text("取消",
                          style: TextStyle(
                              fontSize: 18, fontWeight: FontWeight.normal)))),
              Text(title,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      fontSize: 22,
                      color: Colors.black87,
                      fontWeight: FontWeight.normal)),
              Positioned(
                  right: 2,
                  child: GestureDetector(
                      behavior: HitTestBehavior.translucent,
                      onTap: () {
                        setState(() {
                          Navigator.pop(context, clickedGeoPt);
                        });
                      },
                      child: Container(
                          padding: EdgeInsets.fromLTRB(8, 2, 8, 2),
                          child: Text("确定",
                              style: TextStyle(
                                  fontSize: 17,
                                  fontWeight: FontWeight.normal)))))
            ])));
  }
}
